﻿using DongFangGuoKai.WMS.IApiService;
using DongFangGuoKai.WMSPlatform.Commn;
using DongFangGuoKai.WMSPlatform.Commn.AppsetingsModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using DongFangGuoKai.WMSPlatform.Domain.AGVModels;
using DongFangGuoKai.WMSPlatform.Domain.Enums;

namespace DongFangGuoKai.WMS.ApiService
{
    /// <summary>
    /// AGV接口
    /// </summary>
    public class AGVService : IAGVService
    {
        private ICtmHttpClient _client { get; }

        private string RequstUrl = "http://IP:PORT/rcms/services/rest/hikRpcService/genAgvSchedulingTask";

        public AGVService(ICtmHttpClient client)
        {
            _client = client;
            RequstUrl = ConfigManageHelper.AgvServicePath;
        }


        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="addTaskModel"></param>
        /// <param name="requstUrl"></param>
        /// <returns></returns>
        public ResultRespone CreateTask(AddTaskModel addTaskModel)
        {
            string requstUrl = BulidRequstUrl(AgvRequstType.Create);

            var result = Retry.Execute(() =>
            {
                return _client.Post<ResultRespone>(requstUrl, addTaskModel);
            }, TimeSpan.FromSeconds(5));

            return result;
        }

        /// <summary>
        /// 仓位与容器绑定解绑
        /// </summary>
        /// <param name="addTaskModel"></param>
        /// <param name="requstUrl"></param>
        /// <returns></returns>
        public ResultRespone BindCntrAndUnBindTask(BindCtnrAndBinModel bindCtnrAndBinModel)
        {
            string requstUrl = BulidRequstUrl(AgvRequstType.BindAndUnBind);

            var result = Retry.Execute(() =>
            {
                return _client.Post<ResultRespone>(requstUrl, bindCtnrAndBinModel);
            }, TimeSpan.FromSeconds(5));

            return result;
        }

        /// <summary>
        /// 继续执行
        /// </summary>
        /// <param name="continueTaskModel"></param>
        /// <returns></returns>
        public ResultRespone ContinueRunTask(ContinueTaskModel continueTaskModel)
        {
            string requstUrl = BulidRequstUrl(AgvRequstType.Run);

            var result = Retry.Execute(() =>
            {
                return _client.Post<ResultRespone>(requstUrl, continueTaskModel);
            }, TimeSpan.FromSeconds(5));

            return result;
        }

        /// <summary>
        /// 料箱取放回调
        /// </summary>
        /// <returns></returns>
        public ResultRespone BoxApplyPass(BoxApplyPassModel boxApplyPassModel)
        {
            string requstUrl = BulidRequstUrl(AgvRequstType.Continue);
            var result = Retry.Execute(() =>
            {
                return _client.Post<ResultRespone>(requstUrl, boxApplyPassModel);
            }, TimeSpan.FromSeconds(5));

            return result;
        }

        /// <summary>
        /// 构建请求
        /// </summary>
        /// <param name="requestType"></param>
        /// <returns></returns>
        private string BulidRequstUrl(AgvRequstType agvRequstType)
        {
            switch (agvRequstType)
            {
                //创建
                case AgvRequstType.Create:
                    return RequstUrl + "/rcms/services/rest/hikRpcService/genAgvSchedulingTask";
                //执行
                case AgvRequstType.Run:
                    return RequstUrl + "/rcms/services/rest/hikRpcService/continueTask";
                //取消
                case AgvRequstType.Cancel:
                    return RequstUrl + "/rcms/services/rest/hikRpcService/cancelTas";
                //停止
                case AgvRequstType.Stop:
                    return RequstUrl + "/hikRpcService/stopRobot";
                //恢复
                case AgvRequstType.Recovery:
                    return RequstUrl + "/hikRpcService/resumeRobot";
                //查询状态
                case AgvRequstType.State:
                    return RequstUrl + "/rcms-dps/rest/queryAgvStatus";
                //仓位与容器绑定和解绑
                case AgvRequstType.BindAndUnBind:
                    return RequstUrl + "/rcms/services/rest/hikRpcService/bindCtnrAndBin";
                case AgvRequstType.Continue:
                    return RequstUrl + "/rcms/services/rest/hikRpcService/boxApplyPass";
                default:
                    break;
            }
            return "";
        }
    }
}
